package com.core
{
	import com.config.FrameWorkStates;
	import com.event.GameEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;

	/**
	 * 游戏循环部分
	 * 
	 * 
	 * (需要加一个选关  状态)
	 * (还有一个结算界面)
	 * 
	 **/
	public class GameFrameWork extends Sprite
	{
		//对象
		public var titleScreen:BasicScreen;//进入游戏前的title界面
		public var gameOverScreen:BasicScreen;//游戏结束界面
		public var instructionsScreen:BasicScreen;//游戏说明界面
		public var levelSelectScreen:BasicScreen;//关卡选择
		public var levelInScreen:BasicScreen;//关卡进入游戏的界面
		public var gamePauseScreen:BasicScreen;
		
		public var game:Game;
		
		//控制当前状态参数
		private var lastSystemState:int;//上一状态值
		private var currentSystemState:int;//当前状态值
		private var nextSystemState:int ;//下一状态值 交互用的
		private var systemFunction:Function;//切换状态的函数
		public var frameRate:int;
		
		public var waitTime:int;
		public var waitCount:int = 0;
		
		public var appBackBitmapData:BitmapData;
		public var appBackBitmap:Bitmap;
		public var timerPeriod:Number;
		public var gameTimer:Timer;
		public function GameFrameWork()
		{
			super();
		}
		
		public function init():void
		{
			
		}
		/**
		 * 游戏程序的背景
		 * 
		 * 可以改造为加载库中的位图数据
		 * 
		 * @param	width
		 * @param	height
		 * @param	isTransparent
		 * @param	color
		 */
		public function setApplicationBackGround(width:Number, 
												height:Number, 
												isTransparent:Boolean = false,
												color:uint = 0x000000,bmd:BitmapData = null):void
		{
			appBackBitmapData = new BitmapData(width, height, isTransparent, color);
			if(bmd == null){
				appBackBitmap = new Bitmap(appBackBitmapData);
			}else{
				appBackBitmap = new Bitmap(bmd);
			}
				
			addChild(appBackBitmap);
		}
		/**
		 * 游戏时间即帧的设定
		 */
		public function startTimer():void
		{
			timerPeriod = 1000 / frameRate;
			gameTimer = new Timer(timerPeriod);
			gameTimer.addEventListener(TimerEvent.TIMER, runGame);
			gameTimer.start();
		}
		/**
		 * 游戏运行
		 * @param	e
		 * 
		 * 相当于enterframe  所有的处理都在这里处理掉
		 * 
		 * 
		 * 做一个计秒器
		 */
		public function runGame(e:TimerEvent):void
		{
			systemFunction();
			e.updateAfterEvent();
			
		}
		/**
		 * 状态循环
		 * @stateVal
		 * 
		 **/
		 /**
		 * 尝试增加pause状态
		 **/
		public function switchSystemState(stateVal:int):void{
			lastSystemState = currentSystemState;
			currentSystemState = stateVal;
			trace("currentState :  "+stateVal)
			switch(stateVal)
			{
				case FrameWorkStates.STATE_SYSTEM_WAIT:
					systemFunction = systemWait;
				break; 
				case FrameWorkStates.STATE_SYSTEM_WAIT_FOR_CLOSE:
					systemFunction = systemWaitForClose;
				break;
				case FrameWorkStates.STATE_SYSTEM_TITLE:
					systemFunction = systemTitle;
				break;
				case FrameWorkStates.STATE_SYSTEM_INSTRUCTIONS:
					systemFunction = systemInstructions;
				break;
				case FrameWorkStates.STATE_SYSTEM_NEW_GAME:
					systemFunction = systemNewGame;
				break;
				case FrameWorkStates.STATE_SYSTEM_LEVEL_SELECT:
					systemFunction = systemLevelSelect;
				break;
				case FrameWorkStates.STATE_SYSTEM_NEW_LEVEL:
					systemFunction = systemNewLevel;
				break;
				case FrameWorkStates.STATE_SYSTEM_LEVEL_IN:
					systemFunction = systemLevelIn;
				break;
				case FrameWorkStates.STATE_SYSTEM_GAME_PLAY:
					systemFunction = systemGamePlay;
				break;
				case FrameWorkStates.STATE_SYSTEM_GAME_OVER:
					systemFunction = systemGameOver;
				break;
				case FrameWorkStates.STATE_SYSTEM_PAUSE:
					systemFunction = systemGamePause;
				break;
			}
		}
		
		////////////////////////////////////////////////////
		////处理状态函数//////////////////////////
		////////////////////////////////////////////////////
		private function systemWait():void{
			waitCount++;
			if (waitCount > waitTime)
			{
				waitCount = 0;
				var event:GameEvent = new GameEvent(GameEvent.GAME_WAIT_COMPLETE_EVENT)
				dispatchEvent(event);
			}
		}
		
		private function systemWaitForClose():void{
			
		}
		
		private function systemTitle():void{//打开游戏时的开始界面
			addChild(titleScreen);
			titleScreen.addEventListener(GameEvent.GAME_TITLE_TO_INSTRUCTION_EVENT,onMouseClick);
			titleScreen.addEventListener(GameEvent.GAME_TITLE_TO_NEW_GAME_EVENT,onMouseClick);
			switchSystemState(FrameWorkStates.STATE_SYSTEM_WAIT_FOR_CLOSE);
//			nextSystemState = FrameWorkStates.STATE_SYSTEM_INSTRUCTIONS;
		}
		
		private function systemInstructions():void{//游戏说明
		
		
			addChild(instructionsScreen);
			instructionsScreen.addEventListener(GameEvent.GAME_INSTRUCTION_TO_TITLE_EVENT,onMouseClick)
			switchSystemState(FrameWorkStates.STATE_SYSTEM_WAIT_FOR_CLOSE);
			nextSystemState = FrameWorkStates.STATE_SYSTEM_NEW_GAME;
		}
		
		private function systemNewGame():void{//数据初始化  进入新的关卡
			addChild(game);
			game.newGame();
			
//			game.addEventListener("new_level", newLevelListener);
//			switchSystemState(FrameWorkStates.STATE_SYSTEM_NEW_LEVEL);
			switchSystemState(FrameWorkStates.STATE_SYSTEM_LEVEL_SELECT);
		}
		
		private function systemLevelSelect():void{
			addChild(levelSelectScreen);
			levelSelectScreen.addEventListener(GameEvent.GAME_LEVEL_SELECT,levelSelectListener);
		}
		
		private function systemNewLevel():void{//数据初始化  进入新的关卡
			game.newLevel();
			switchSystemState(FrameWorkStates.STATE_SYSTEM_LEVEL_IN);
		}
		/**
		 * 做一个秒计时
		 **/
		 private var _lastTime:int;
		 private var _currentTime:int;
		private function systemGamePlay():void{//游戏主体部分
			game.runGame();
			addEventListener(GameEvent.GAME_PLAY_TO_PAUSE,onGamePlayToPause);
			_currentTime = getTimer();
			if(_currentTime-_lastTime>=1000){
				game.runGameSecond();
				_lastTime = _currentTime;
			}
		}
		
		private function systemGameOver():void{//游戏结束
			removeChild(game);
			addChild(gameOverScreen);
			switchSystemState(FrameWorkStates.STATE_SYSTEM_WAIT_FOR_CLOSE);
			nextSystemState = FrameWorkStates.STATE_SYSTEM_TITLE;
		}
		
		private function systemLevelIn():void{//上一关卡和下一关卡衔接的部分
			removeChild(levelSelectScreen);
			addChild(levelInScreen);
//			waitTime = 30;
			switchSystemState(FrameWorkStates.STATE_SYSTEM_WAIT);
			nextSystemState = FrameWorkStates.STATE_SYSTEM_GAME_PLAY;
			addEventListener(GameEvent.GAME_WAIT_COMPLETE_EVENT,waitCompleteListener,false,0,true);
		}
		
		private function systemGamePause():void{//游戏暂停
			addChild(gamePauseScreen);
			nextSystemState = FrameWorkStates.STATE_SYSTEM_GAME_PLAY;
			addEventListener(GameEvent.GAME_PAUE_TO_PLAY,onGamePauseToPlay);
		}
		private function onGamePlayToPause():void{
			gameTimer.stop();
		}
		private function onGamePauseToPlay():void{
			gameTimer.start();
		}
		
		
		private function onMouseClick(event:GameEvent):void{
			switch(event.type){
				case GameEvent.GAME_TITLE_TO_INSTRUCTION_EVENT:{
					nextSystemState = FrameWorkStates.STATE_SYSTEM_INSTRUCTIONS;
					removeChild(titleScreen);
					break;
				}
				case GameEvent.GAME_TITLE_TO_NEW_GAME_EVENT:{//选择关卡
					nextSystemState = FrameWorkStates.STATE_SYSTEM_NEW_GAME;
//					nextSystemState = FrameWorkStates.STATE_SYSTEM_LEVEL_SELECT;
					removeChild(titleScreen);
					break;
				}
				case GameEvent.GAME_INSTRUCTION_TO_TITLE_EVENT:{
					nextSystemState = FrameWorkStates.STATE_SYSTEM_TITLE;
					removeChild(instructionsScreen);
					break;
				}
			}
			switchSystemState(nextSystemState);
		}
		private function waitCompleteListener(event:GameEvent):void{
			switch(lastSystemState)
			{
				case FrameWorkStates.STATE_SYSTEM_LEVEL_IN :
					removeChild(levelInScreen);
					game.startGame( );
				break;
			}
			removeEventListener(GameEvent.GAME_WAIT_COMPLETE_EVENT, waitCompleteListener);
			switchSystemState(nextSystemState);
		}
		
		public function newLevelListener(e:Event):void {
			switchSystemState(FrameWorkStates.STATE_SYSTEM_NEW_LEVEL);
		}
		
		public function levelSelectListener(e:GameEvent):void{
			switchSystemState(FrameWorkStates.STATE_SYSTEM_NEW_LEVEL);
		}
	}
}